package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentRepairProgress;
import com.mxpio.erp.common.equipment.service.EquipmentRepairProgressService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentRepairProgressController}", description = "设备维修-维修操作进度")
@RestController
@RequestMapping("/erp/equipment/spotrepair/progress/")
public class EquipmentRepairProgressController {

    @Autowired
    private EquipmentRepairProgressService service;


    @GetMapping("list")
    @Operation(summary = "设备维修-维修操作进度列表", description = "获取设备维修-维修操作进度列表（无分页）", method = "GET")
    public Result<List<EquipmentRepairProgress>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentRepairProgress> list = service.list(EquipmentRepairProgress.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "设备维修-维修操作进度列表", description = "获取设备维修-维修操作进度列表（分页）", method = "GET")
    public Result<Page<EquipmentRepairProgress>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentRepairProgress> pageResult = service.listPage(EquipmentRepairProgress.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo工单编码获取设备维修-维修操作进度列表", description = "根据bizNo工单编码获取设备维修-维修操作进度列表", method = "GET")
    public Result<List<EquipmentRepairProgress>> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria criteria = Criteria.create();
        criteria.addCriterion("bizNo", Operator.EQ,bizNo);
        List<EquipmentRepairProgress> list = service.list(EquipmentRepairProgress.class, criteria);
        return Result.OK(list);
    }

    @RequestMapping("add")
    @Operation(summary = "保存设备维修-维修操作进度", description = "保存设备维修-维修操作进度")
    public Result<EquipmentRepairProgress> add(@RequestBody EquipmentRepairProgress equipmentUpkeepTaskDetail) {
        service.save(equipmentUpkeepTaskDetail);
        return Result.OK(equipmentUpkeepTaskDetail);
    }

    @RequestMapping("save")
    @Operation(summary = "批量保存设备维修-维修操作进度", description = "批量保存设备维修-维修操作进度")
    public Result<List<EquipmentRepairProgress>> save(@RequestBody List<EquipmentRepairProgress> details) {
        service.save(details);
        return Result.OK(details);
    }
}
